package util;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;
import java.util.Scanner;

/**
 * 根据用户id查出，所有的笔记信息
 */
public class ChaUserId {
    public static void main(String[] args) throws Exception {
        Scanner s=new Scanner(System.in);
        System.out.print("请输入用户id：");
        int id = s.nextInt();
        //创建连接池
        Properties properties=new Properties();
        //把druid.properties.properties中的内容复制到properties对象中
        properties.load(new FileInputStream("D:\\study\\Java\\YuQue\\src\\druid.properties.properties"));
        //dataSource 就是连接池
        DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
        //从连接池中获取连接
        Connection connection = dataSource.getConnection();
        //sql语句
        String str="SELECT `name`,note.* from note,user WHERE `user`.id=note.person_id AND person_id=?";
        //拉数据
        PreparedStatement preparedStatement = connection.prepareStatement(str);
        //给?赋值
        preparedStatement.setInt(1,id);
        //查sql语句
        ResultSet resultSet = preparedStatement.executeQuery();
        System.out.println("id"+"\t"+"作者"+"\t"+"题目"+"\t"+"日期"+"\t"+"内容"+"\t"+"用户id");
        while (resultSet.next()){
            int id01 = resultSet.getInt("id");
            String author = resultSet.getString("author");
            String title = resultSet.getString("title");
            String datetime = resultSet.getString("datetime");
            String content = resultSet.getString("content");
            String person_id = resultSet.getString("person_id");
            System.out.println(id01+"\t"+author+"\t"+title+"\t"+datetime+"\t"+content+"\t"+person_id);
        }
    }
}
